package ve.com.oauth.controlador;

import java.io.IOException;
import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import twitter4j.conf.ConfigurationBuilder;
import twitter4j.internal.http.HttpResponse;
import ve.com.oauth.entity.Login;

@ManagedBean(name = "loginBean")
@SessionScoped
public class LoginBean implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// Constantes

	static String CONSUMER_KEY = "qHlOQ0rqEbQeOXlMrZSbfA";
	static String CONSUMER_SECRET = "6P72EJNm6eKk93oyhxhIY8IVLt5QK0JTgQ5Ih30j8";
	static String ACCESS_TOKEN = "42146959-moYk6tH6L6b8GcLLU3wIpWhSkcT5CtA0uaDalrHNy";
	static String ACCESS_TOKEN_SECRET = "RVW8taKWLWitEUwz7OT77KsZJEvfIyIwVADOpVJ92tP0c";

	static final String CALLBACK_URL = "http://env-4137174.jelastic.servint.net/oauthTwitter/";

	Twitter twitter;
	RequestToken requestToken;
	AccessToken accessToken;

	// Login
	private Login login;

	@PostConstruct
	public void inicializar() {
		login = new Login();
	}

	public Login getLogin() {
		return login;
	}

	public void setLogin(Login login) {
		this.login = login;
	}

	public String ingresarUsuario() throws IOException, TwitterException {

		System.out.println("*****************************");
		System.out.println("***Login Twitter con OAuth***");
		System.out.println("*****************************");

	
		 try {
		 
		  twitter = new TwitterFactory().getInstance();
		  twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
		 
		  requestToken = twitter.getOAuthRequestToken();
		 
		  System.out.println("ANTES Token:" + requestToken.getToken() );
		  System.out.println("ANTES TokenSecret:" + requestToken.getTokenSecret());
		  
		 /* System.out.println("Authorization URL: \n" +
		  requestToken.getAuthorizationURL()); */
		  
		  AccessToken accessToken = new AccessToken(ACCESS_TOKEN, ACCESS_TOKEN_SECRET);

		  twitter.setOAuthAccessToken(accessToken);
		  
		  ExternalContext ec = FacesContext.getCurrentInstance()
		  .getExternalContext();
		  ec.redirect(requestToken.getAuthorizationURL());
		  
		  twitter.getOAuthAccessToken(requestToken);
			 
		 System.out.println("Usuario "+twitter.getAccountSettings().getScreenName());
		  
		/*  AccessToken accessToken = null;
		  
		  accessToken = twitter.getOAuthAccessToken(ACCESS_TOKEN, ACCESS_TOKEN_SECRET);*/
		 
		  
	/* 
		 * System.out.println("ACCESSTOKEN "+accessToken);
		 * 
		 * //twitter.getOAuthAccessToken(requestToken);
		 * 
		 * twitter.getAccountSettings().getScreenName();
		 * 
		 * System.out.println("SCREENNAME "+
		 * twitter.getAccountSettings().getScreenName()); } catch
		 * (TwitterException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); }
		 */
		/*ConfigurationBuilder cb = new ConfigurationBuilder();
		cb.setOAuthConsumerKey(CONSUMER_KEY);
		cb.setOAuthConsumerSecret(CONSUMER_SECRET);
		cb.setOAuthAccessToken(ACCESS_TOKEN);
		cb.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET); 
		
		TwitterFactory tf = new TwitterFactory(cb.build());
		twitter = tf.getInstance();

		try {
			
			requestToken = twitter.getOAuthRequestToken();
			
			String url = requestToken.getAuthorizationURL();
			System.out.println("URL "+ url);
			requestToken = twitter.getOAuthRequestToken();
			
			ExternalContext ec = FacesContext.getCurrentInstance()
					.getExternalContext();
			ec.redirect(requestToken.getAuthorizationURL());
			
			twitter.getAccountSettings().getScreenName();
			System.out.println("SCREENNAME "
					+ twitter.getAccountSettings().getScreenName());*/
		} catch (TwitterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return "";
	}

}
